$(document).ready(function() {
    /* toggle layout */
	$('#btnToggle').click(function(){
		if ($(this).hasClass('on')) {
			$('#main .col-md-6').addClass('col-md-4').removeClass('col-md-6');
			$(this).removeClass('on');
		} else {
			$('#main .col-md-4').addClass('col-md-6').removeClass('col-md-4');
			$(this).addClass('on');
		}
	});
	
	$(".dropdown-menu li a").click(function(){
		var selText = $(this).text();
		console.log(selText);
		$(this).parents('.btn-group').find('.dropdown-toggle').html(selText+' <span class="caret"></span>');
		
	});
});

(function($) {
	$.fn.buttonSelect = function() {
        this.hide().wrap('<div class="btn-group"/>');
        var select = this.parent();
        var selectedOption=this.find('option[selected]').length>0?this.find('option[selected]'):this.find('option:nth(0)');
        var currentValue = selectedOption.val();
        var currentText = selectedOption.text();
		select.html(
            '<input type="hidden" value="' + this.val() + '"/>'+
            '<a class="btn dropdown-toggle" data-toggle="dropdown" href="javascript:;">'+
                '<span>'+currentText+'</span>'+
                '&nbsp;&nbsp;<span class="caret"></span>'+
            '</a>'+
            '<ul class="dropdown-menu"></ul>');

        var dropdownMenu=select.find('.dropdown-menu');
        this.find('option').each(function(o, q) {
            dropdownMenu.append('<li><a href="javascript:;" data-value="' + $(q).attr('value') + '">' + $(q).text() + '</a></li>');
        });

        var hidden=select.find('input[type=hidden]');
        var label=select.find('.btn span:nth(0)');
        dropdownMenu.find('a').click(function() {
			hidden.val($(this).data('value')).change();
            label.text($(this).text());
        });

		return this;
	};
})(jQuery);

(function() {
    'use strict';
    var statefulDropdowns = $('li.dropdown.stateful'),
        dropdownToggle = statefulDropdowns.find('a.dropdown-toggle'),
        caret = dropdownToggle.find('b.caret'),
        dropdown = statefulDropdowns.find('ul.dropdown-menu');
 
    dropdown.find('li > a').click(function(e) {
        e.preventDefault();
        var me = $(this),
            clickedLinkText = me.text(),
            dropdownToggleText = dropdownToggle.text();
 
        me.text(dropdownToggleText);
        dropdownToggle.text(clickedLinkText);
        caret.appendTo(dropdownToggle);
    });
}());


$("#button-login").on("click", function () {
	console.log("loginCommand clicked");
	var request = $.ajax({
		url: "/servlet/login",
		type: "GET",
//		data: parameter
	});
	
	request.success(function (data, textStatus, jqXHR) {
		console.log("ok");
		console.log(data);
		$("#modal-login-body").append(data);
	});
	
});
